<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/17
 * Time: 20:28
 */
namespace App\Admin\Controllers;

use App\Admin\Traits\UserTrait;
use App\Helper;
use App\Http\Controllers\Controller;
use App\Models\GroupConfig;
use App\Models\System;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use function Symfony\Component\String\u;

/**
 * 工具
 * Class ToolsController
 * @package App\Admin\Controllers
 */
class GroupController extends Controller {

    use UserTrait;

    public function index(Content $content)
    {
        $uid = $this->getUserId();
        $model = GroupConfig::where("user_id", $uid)
            ->first();
        if (!$model) {
            $model = GroupConfig::createGc($uid);
        }
        return $content
            ->title("系统管理")
            ->body($this->form($model));

    }

    protected function form(GroupConfig $model)
    {
        $form = new Form($model);
        $form->setTitle("修改群发配置");
        Helper::formLoadModel($form, $model, ['interval']);
        $form->setAction("/admin/group?id=" . $model->id);
        return $form;
    }

    public function store(Request $request)
    {
        $id = $request->get("id");
        $data = $request->post();
        /** @var System $model */
        $model = GroupConfig::where("user_id", $this->getUserId())
            ->where("id", $id)
            ->first();
        if ($model) {
            $model->update($data);
        }
        return back();
    }



}